package cn.com.nes.site.action.templetedesign;


import cn.com.nes.common.util.DBUtils;
import cn.com.nes.common.util.OssUtil;
import cn.com.nes.common.util.Page;
import cn.com.nes.common.util.ParameterUtil;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.service.templatedesign.HDevicemodelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/houseDevice")
public class HDevicemodelController {
    @Autowired
    private HDevicemodelService hDevicemodelService;

    /**
     * 添加设备模板
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(method = RequestMethod.POST,value = "/addHouseDeviceModel")
    @ResponseBody
    public PageData addHouseDeviceModel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        int count = hDevicemodelService.addHouseDeviceModel(req) ;
        if (count!=0){
            res.setRetCode(0);
            res.setRetMessage("操作成功!");
        }else {
            res.setRetCode(0);
            res.setRetMessage("该型号已存在!");
        }
        return res;
    }

    /**
     * 编辑设备信息
     * 已测试
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value ="/updateHouseDeviceModel")
    @ResponseBody
    public PageData updateHouseDeviceModel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
      int count = hDevicemodelService.updateHouseDeviceModel(req);
      if (count!=0){
          res.setRetCode(0);
          res.setRetMessage("操作成功!");
      }
        return res;
    }

    /**
     * 删除设备信息 isdelete 1已删除，0未删除
     * 已测试
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/deleteHouseDeviceModel")
    @ResponseBody
    public PageData deleteHouseDeviceModel(HttpServletRequest request, HttpServletResponse response) throws Exception{
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
        int count=hDevicemodelService.deleteHouseDeviceModelById(req);
        if (count!=0){
            res.setRetCode(0);
            res.setRetMessage("操作成功!");
        }
        return res;
    }

    /**
     * 根据设备厂家id查询设备型号的名字和设备型号的id
     * @param request
     * @param response
     * @return
     */
  @RequestMapping(method = RequestMethod.POST,value = "/selectSbhxAndSbxh_id")
  @ResponseBody
   public PageData selectSbhxAndSbxh_id(HttpServletRequest request, HttpServletResponse response) throws Exception{
      DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
      PageData res = new PageData() ;
      if (req_Status.getRetCode() != 0) {
          res = new PageData();
          res.setRetCode(req_Status.getRetCode()) ;
          res.setRetMessage(req_Status.getRetMessage());
          return res;
      }
      DataReq req = req_Status.getReq();
      List<Map<String,Object>> list=hDevicemodelService.selectSbxh_idAndSbxhname(req);
      if (list.size()!=0){
          res.setRetCode(0);
          res.setRetMessage("操作成功!");
      }else{
          res.setRetCode(0);
          res.setRetMessage("设备型号不存在!");
      }
      res.setPage(list);
        return res;

   }

    /**
     * 根据设备厂家id和设备类型id查询指定设备信息
     * 已测试
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.POST,value = "/selectHouseDeviceModel")
    @ResponseBody
    public PageData selectAllHouseDeviceModel(HttpServletRequest request, HttpServletResponse response)throws Exception{
        DataRes req_Status = DBUtils.setRemoteVisit(request, response,true);
        PageData res = new PageData() ;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode()) ;
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();
       List<Map<String,Object>> list=hDevicemodelService.selectHouseDeviceModel(req);
       int pagesize= ParameterUtil.getInteger(req,"pagesize");
       int count=hDevicemodelService.selectAllList(req);
       int pageCount=count/pagesize+1;
        if (list.size()!=0){
            res.setRetCode(0);
            res.setRetMessage("操作成功!");
        }else{
            res.setRetCode(0);
            res.setRetMessage("ok");
        }
        res.setPagecount(pageCount);
        res.setRecordcount(count);
        res.setPage(list);
        return res;
}

}
